<template>
  <div class="login-page">
    <div class="back">
      <!-- .native绑定组件的原生事件，属于组件根元素 -->
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <geek-icon @click.native="goback" name="esay-close"></geek-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <van-form ref="form">
      <van-field
        placeholder="手机号"
        v-model="form.mobile"
        :rules="rules.mobile"
        name="mymobile"
      />
      <van-field placeholder="验证码" v-model="form.code" :rules="rules.code">
        <template #button>
          <span class="send" @click="sendMsg">{{second>0?second:'发送验证码'}}</span>
        </template>
      </van-field>
    </van-form>
    <van-button @click="login">登录</van-button>
  </div>
</template>
<script>
import { login, sendSMS } from '@/api/user'
export default {
  created () {
    console.log(this.$store)
  },
  methods: {
    async sendMsg () {
      // 只要还在倒计时 就直接返回，不做任何处理。
      if (this.second > 0) return

      // 1.校验手机号是否正确
      console.log(this.$refs.form.validate)
      //   单个校验  必须加上name属性
      const err = await this.$refs.form.validate('mymobile')
      if (err) return this.$toast.fail(err)

      // 2.请求验证码
      const [error, data] = await sendSMS(this.form.mobile)
      if (error) this.$toast.fail(error.message)
      console.log(data)
      if (data.data.message === 'OK') {
        //
        this.second = 60
        clearInterval(this.timer)
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
          }
        }, 1000)
      }
    },
    goback () {
      this.$router.go(-1)
    },
    async login () {
      // 在进行提交之前进行校验，如果检验成功 则继续提交，如果校验不成功，中止提交。
      console.log(this.$refs.form)
      const err = await this.$refs.form.validate()
      if (err) return

      //   提交手机号和验证码
      const [error, data] = await login(this.form.mobile, this.form.code)
      if (error) return this.$toast.fail(error.message)
      console.log(data)

      //   1.首先存储Token
      // console.log(data.data.data.token)
      console.log(data)
      this.$store.commit('user/setToken', data.data.token)
      this.$store.commit('user/setRefreshToken', data.data.refresh_token)

      console.log(this.$store.mutations)
      //   2.返回原来页面
      console.log(this.$route.query.returnUrl)
      this.$router.replace(this.$route.query.returnUrl || '/')
    }
  },
  data () {
    return {
      second: 0,
      timer: null,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码是6个数字' }
        ]
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .geek-icon {
      font-size: 20px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after {
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #ff9999, #ffa179);
  }
}

.send {
  font-size: 12px;
  color: #a5a6ab;
}
</style>
